package com.scnlf.scnlf.portal.core.business.loan.service.impl;

import com.scnlf.scnlf.portal.core.business.loan.entity.LoanTicketEntity;
import com.scnlf.scnlf.portal.core.business.loan.repo.LoanTicketRepo;
import com.scnlf.scnlf.portal.core.business.loan.service.ILoanTicketService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class LoanTicketServiceImpl implements ILoanTicketService {

  private final LoanTicketRepo loanTicketRepo;

  @Override
  public LoanTicketEntity getById(Long id) {
    return loanTicketRepo.findById(id).orElseThrow(() -> new RuntimeException("not find ticket"));
  }

  @Override
  public Long calculateLeaveQuantity(LoanTicketEntity loanTicket, Long usedQuantity) {
    return loanTicket.getLoanQty() - usedQuantity;
  }

  @Override
  public void validEnableLoan(LoanTicketEntity loanTicket, Long quantity, Long usedQuantity) {
    long leaveQuantity = calculateLeaveQuantity(loanTicket, usedQuantity) - quantity;
    if(leaveQuantity < 0){
      throw new RuntimeException("券单可约数量不足");
    }
  }

}
